//
//  WZLoginViewController.swift
//  OfficeExaminationSwift
//
//  Created by Yeeewz on 2018/1/8.
//  Copyright © 2018年 Yeeewz. All rights reserved.
//

import UIKit
import SwiftyJSON
import ObjectMapper

class WZLoginViewController: WZBaseWhiteNavViewController , UITextFieldDelegate{

    var phoneNumberTextField : UITextField!
    var passwordTextField : UITextField!
    var rememberPasswordBtn : UIButton!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.navigationItem.title = "登录"
     
        self.view.backgroundColor = KCOLORWHITE
        self.initViews()
        
    }

    func initViews(){
        let logoImageView = UIImageView()
        self.view.addSubview(logoImageView)
        logoImageView.image = UIImage(named:"logo")
        logoImageView.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(KLineY(Y: 120))
            make.right.equalTo(KLineX(X: -143))
            make.height.width.equalTo(KLineY(Y: 90))
        }
        
        
        //手机号
        let phoneLabel = UILabel()
        phoneLabel.text = "手机号:"
        phoneLabel.font = KFONT(fontSize: 15)
        phoneLabel.textColor = KCOLORWITHRGB(red: 79, green: 80, blue: 89)
        self.view.addSubview(phoneLabel)
        phoneLabel.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(logoImageView.snp.bottom).offset(KLineY(Y: 49))
            make.left.equalTo(KLineX(X: 15))
            make.width.equalTo(KLineX(X: 16 * 4))
            make.height.equalTo(KLineY(Y: 20))
        }
        self.phoneNumberTextField = UITextField()
        self.initTextField(textField: self.phoneNumberTextField,fontSize: KLineY(Y: 15), placeholderTitle: "请输入手机号", borderStyle: UITextBorderStyle.none, keyboardType: UIKeyboardType.phonePad)
        self.phoneNumberTextField.text = "18530815354"
        self.view.addSubview(self.phoneNumberTextField)
        self.phoneNumberTextField.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(phoneLabel.snp.right)
            make.centerY.equalTo(phoneLabel.snp.centerY)
            make.right.equalTo(KLineX(X: -15))
            make.height.equalTo(KLineY(Y: 35))
        }
        let firstLineView = UIView()
        firstLineView.backgroundColor = KCOLORWITHRGB(red: 219, green: 223, blue: 225)
        self.view.addSubview(firstLineView)
        firstLineView.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(KLineX(X: 15))
            make.height.equalTo(KLineY(Y: 1))
            make.right.equalTo(KLineX(X: -15))
            make.top.equalTo(phoneLabel.snp.bottom).offset(KLineY(Y: 17))
        }
        let passwordLabel = UILabel()
        passwordLabel.text = "密   码:"
        passwordLabel.font = KFONT(fontSize: 15)
        passwordLabel.textColor = KCOLORWITHRGB(red: 79, green: 80, blue: 89)
        self.view.addSubview(passwordLabel)
        passwordLabel.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(firstLineView.snp.bottom).offset(KLineY(Y: 17))
            make.left.equalTo(KLineX(X: 15))
            make.width.equalTo(KLineX(X: 16 * 4))
            make.height.equalTo(KLineY(Y: 20))
        }
        //密码
        self.passwordTextField = UITextField()
        self.initTextField(textField: self.passwordTextField, fontSize: 15, placeholderTitle: "请输入密码", borderStyle:
            UITextBorderStyle.none, keyboardType: UIKeyboardType.default)
        self.passwordTextField.isSecureTextEntry  = true
        self.passwordTextField.text = "123456"
        self.view.addSubview(self.passwordTextField)
        self.passwordTextField.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(self.phoneNumberTextField.snp.left)
            make.centerY.equalTo(passwordLabel.snp.centerY)
            make.right.equalTo(KLineX(X: -15))
            make.height.equalTo(KLineY(Y: 35))
        }
        let secondLineView = UIView()
        secondLineView.backgroundColor = KCOLORWITHRGB(red: 219, green: 223, blue: 225)
        self.view.addSubview(secondLineView)
        secondLineView.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(KLineX(X: 15))
            make.height.equalTo(KLineY(Y: 1))
            make.right.equalTo(KLineX(X: -15))
            make.top.equalTo(passwordLabel.snp.bottom).offset(KLineY(Y: 17))
        }
        //记住密码
        self.rememberPasswordBtn = UIButton(type: UIButtonType.custom)
        self.rememberPasswordBtn.setImage(UIImage(named:"jizhu01"), for: UIControlState.normal)
        self.rememberPasswordBtn.setImage(UIImage(named:"jizhu02"), for: UIControlState.selected)
        self.rememberPasswordBtn.addTarget(self, action:#selector(self.rememberPasswordBtnEvent), for: UIControlEvents.touchUpInside)
        self.view.addSubview(self.rememberPasswordBtn)
        self.rememberPasswordBtn.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(KLineX(X: 15))
            make.top.equalTo(secondLineView.snp.bottom).offset(KLineY(Y: 5))
            make.height.width.equalTo(KLineY(Y: 40))
        }
        let rememberPWLabel = UILabel()
        rememberPWLabel.text = "记住密码"
        rememberPWLabel.font = KFONT(fontSize: 13)
        rememberPWLabel.textColor = KCOLORWITHRGB(red: 77, green: 77, blue: 77)
        self.view.addSubview(rememberPWLabel)
        rememberPWLabel.snp.makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.rememberPasswordBtn.snp.centerY)
            make.left.equalTo(self.rememberPasswordBtn.snp.right)
        }
        //登录
        let loginBtn = UIButton(type: UIButtonType.custom)
        loginBtn.setTitle("登录", for: UIControlState.normal)
        loginBtn.addTarget(self, action:#selector(self.loginEvent), for: UIControlEvents.touchUpInside)
        loginBtn.backgroundColor = KCOLORWITHRGB(red: 75, green: 141, blue: 250)
        loginBtn.layer.cornerRadius = KLineY(Y: 5)
        self.view.addSubview(loginBtn)
        loginBtn.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.rememberPasswordBtn.snp.bottom).offset(KLineY(Y: 20))
            make.left.equalTo(KLineX(X: 15))
            make.right.equalTo(KLineX(X: -15))
            make.height.equalTo(KLineY(Y: 50))
        }
        //注册
        let registerBtn = UIButton(type: UIButtonType.custom)
        registerBtn.setTitle("立即注册", for: UIControlState.normal)
        registerBtn.titleLabel?.font = KFONT(fontSize: 14)
        registerBtn.setTitleColor(KCOLORWITHRGB(red: 75, green: 141, blue: 250), for: UIControlState.normal)
        registerBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
        registerBtn.addTarget(self, action: #selector(self.registerBtnEvent), for: UIControlEvents.touchUpInside)
        self.view.addSubview(registerBtn)
        registerBtn.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(KLineX(X: 15))
            make.top.equalTo(loginBtn.snp.bottom).offset(KLineY(Y: 26))
            make.height.equalTo(KLineY(Y: 16))
            make.width.equalTo(KLineX(X: 100))
        }
        //忘记密码
        let forgotPasswordBtn = UIButton(type: UIButtonType.custom)
        forgotPasswordBtn.titleLabel?.font = KFONT(fontSize: 14)
        forgotPasswordBtn.setTitleColor(KCOLORWITHRGB(red: 77, green: 77, blue: 77), for: UIControlState.normal)
        forgotPasswordBtn.setTitle("忘记密码", for: UIControlState.normal)
        forgotPasswordBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.right
        forgotPasswordBtn.addTarget(self, action: #selector(self.forgotPasswordBtnEvent), for: UIControlEvents.touchUpInside)
        self.view.addSubview(forgotPasswordBtn)
        forgotPasswordBtn.snp.makeConstraints { (make) -> Void in
            make.centerY.equalTo(registerBtn.snp.centerY)
            make.width.equalTo(KLineX(X: 90))
            make.right.equalTo(KLineX(X: -15))
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //记住密码
    @objc func rememberPasswordBtnEvent() -> Void {
        self.rememberPasswordBtn.isSelected = !self.rememberPasswordBtn.isSelected
        
    }
    //登录
    @objc func loginEvent(){
        print("登录")
        
    loginProvider.request(.login(self.phoneNumberTextField.text!,self.passwordTextField.text!)) { (result) in
        
            if case let .success(response) = result{
                //解析数据
                let data = try? response.mapJSON() //解析数据 获取data 类型
                let json = JSON(data!) //data 转json
                
                let jsonStr = "\(json)"//json 类型转  json 字符串 否则崩溃 后面无法解析
                print(jsonStr)
                let loginModel: WZLoginModel = Mapper().map(JSONString: jsonStr)!
                
                print(loginModel.userInfo?.uid as Any!)

                if loginModel.result == "0"{
                    print("登录成功")
                }
            }
        }
    }
    //注册
    @objc func registerBtnEvent(){
        print("注册")
    }
    //忘记密码
    @objc func forgotPasswordBtnEvent(){
        print("忘记密码")
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    func initTextField(textField : UITextField ,fontSize : CGFloat, placeholderTitle : NSString , borderStyle : UITextBorderStyle , keyboardType : UIKeyboardType) -> Void {
        textField.font = UIFont.systemFont(ofSize: fontSize as CGFloat)
        textField.placeholder = placeholderTitle as String
       textField.borderStyle = borderStyle
        textField.keyboardType = keyboardType
        textField.delegate = self
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
